<?php

namespace common\observer\task\version;

use common\model\TaskModel;
use teamones\dbeventObserver\interfaces\SplObserver;
use teamones\dbeventObserver\interfaces\SplSubject;
use support\SingletonTrait;
use Throwable;

/**
 * 内部任务版本上传记录版本id
 */
class VersionUploadedRecordUploadedVersion implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        if (!isset($updateData['is_uploaded']) || $updateData['is_uploaded'] !== 'yes') {
            return;
        }
        $version = $subject->getSubjectData();

        try {
            model(TaskModel::class)
                ->execute("UPDATE task SET 
                json = JSON_SET(IFNULL(json,'{}'), '$.task_uploaded_version_id', 
                    {$version['id']})
                WHERE id = {$version['link_id']}");
        } catch (Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
